package NWish.properties
{
	import NWish.Interface.ILogger;
	
	
	import NWish.NWEvent;
	import NWish.properties.animator.SmoothSkeletonAnimatorProperties;
	
	import away3d.animators.SmoothSkeletonAnimator;
	import away3d.animators.data.SkeletonAnimationState;
	import away3d.entities.Mesh;
	
	import org.aswing.JMenu;
	import org.aswing.JMenuItem;
	import org.aswing.JToggleButton;

	public class AnimatorPropertiesTypes
	{
		//private var _prient;
		private var _window;
		
		private var _types:Array;
		
		private var _ssa:JMenuItem;

		
		public function AnimatorPropertiesTypes(parent,window)
		{
			//_prient = parent;
			_window = window;
			
			//
			var am:JMenu = parent.getPlugin('MenuBar').animatorMenu;
			_ssa = am.addMenuItem("SmoothSkeletonAnimator");
			_ssa.addActionListener(actionSmoothSkeletonAnimator);
			_ssa.setEnabled(false);
			
			parent.addEventListener(NWEvent.SELSCT_OBJECT,onSelectObject);
			
			
			//
			var animator:JToggleButton = new JToggleButton('    animator    ');
			window.buttonGroup.append(animator);
			animator.x = 5;
			animator.y = 18;
			animator.pack();
			animator.height = 16;
			window.panel.append(animator);
			animator.addActionListener(onAnimator);
			
			//
			_types = 
				[new SmoothSkeletonAnimatorProperties(parent)];
		}
		
		private function onAnimator(e)
		{
			if((e.target as JToggleButton).isSelected())
			{
				_window.currPropertiesTypes = this;
				//subMeshs.visible = false;
			}
		}
		
		
		public function get types()
		{
			return _types;
		}
		
		public function getPropertiesType(obj:Object)
		{
			var aplist = new AnimatorPropertiesList(null,_types);
			
			//
			var b = false;
			for each (var p in _types)
			{
				if(p.hasAnimactor(obj))
				{
					return aplist;
				}
			}
			
			//
			return null;
		}
		
		public function toString()
		{
			return 'animator properties';
		}
		
		
		//
		var _selectObject;
		public function onSelectObject(e:NWEvent)
		{
			_selectObject = null;
			_ssa.setEnabled(false);
			
			//
			var obj = e.objects;
			if(e.objects is Array && e.objects.length == 1)
				obj = e.objects[0];
			else if(e.objects is Array && e.objects.length > 1)
				return;
			
			if(obj is Mesh == false)
			{
				return;
			}
			if(obj.extra.meshFileName == null || obj.extra.meshFileName.search('.md5mesh') == -1)
			{
				return;
			}
			if(obj.extra.smoothSkeletonAnimator)
			{
				return;
			}
			
			_selectObject = obj;
			
			_ssa.setEnabled(true);
		}
		
		public function actionSmoothSkeletonAnimator(e)
		{
			if(_selectObject == null)
			{
				ILogger.log('please select a md5 Mesh!');
			}
			
			//
			_selectObject.extra.smoothSkeletonAnimator 
				= new SmoothSkeletonAnimator(SkeletonAnimationState(_selectObject.animationState));
			
			ILogger.log('create SmoothSkeletonAnimator!',_selectObject);
			_ssa.setEnabled(false);

		}
		
	}
}


import NWish.json.OrderDict;
import NWish.properties.Properties;

import away3d.containers.Scene3D;



class AnimatorPropertiesList extends Properties
{
	private var _aps;
	
	public function AnimatorPropertiesList(parent,aps)
	{
		super(parent);	
		
		_aps = aps;
	}

	public function add(ap)
	{
		_aps.push(ap);
	}
	
	override public function toString()
	{
		return "Animators";
	}
	
	override public function propertiesToDict(obj,dict:OrderDict,read_only = false,saving = false)
	{
		if(read_only == false)
		{
			for each(var ap in _aps)
			{
				if(ap.hasAnimactor(obj))
				{
					var sub = new OrderDict();
					dict.push(ap.toString(),sub);
					ap.propertiesToDict(obj,sub,read_only,saving);
				}
			}
		}
		else
		{
			for each(var ap in _aps)
			{
				if(ap.hasAnimactor(obj))
				{
					var sub = dict.getValue(ap.toString())
					ap.propertiesToDict(obj,sub,read_only,saving);
				}
			}
		}
	}
	
	override public function dictToProperties(dict,obj,scene:Scene3D,isloading)
	{
		for each(var ap in _aps)
		{
			var sub = dict[ap.toString()];
			
			if(sub)
				ap.dictToProperties(sub,obj,scene,isloading);
		}
	}
}


